//
//  BaseToolBar.m
//  Weibo
//
//  Created by Anasue on 10/2/15.
//  Copyright © 2015 vvusu. All rights reserved.
//

#import "BaseToolBar.h"
#import "Status.h"

@interface BaseToolBar ()
@property (nonatomic, strong) NSMutableArray *btns;
@property (nonatomic, strong) UIButton *repostBtn;
@property (nonatomic, strong) UIButton *commentBtn;
@property (nonatomic, strong) UIButton *attitudeBtn;

@end
@implementation BaseToolBar


- (NSMutableArray *)btns
{
    if (_btns == nil) {
        _btns = [NSMutableArray array];
    }
    return _btns;
}

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        self.repostBtn = [self setupBtnWithIcon:@"timeline_icon_retweet" title:@"转发"];
        self.commentBtn = [self setupBtnWithIcon:@"timeline_icon_comment" title:@"评论"];
        self.attitudeBtn = [self setupBtnWithIcon:@"timeline_icon_unlike" title:@"赞"];
    }
    return self;
}
/**
 *  增加按钮
 *
 */
- (UIButton *)setupBtnWithIcon:(NSString *)icon title:(NSString *)title
{
    UIButton *btn = [[UIButton alloc]init];
    [btn setImage:[UIImage imageWithName:icon] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage resizeImage:@"common_card_bottom_background_highlighted"] forState:UIControlStateHighlighted];
    btn.adjustsImageWhenHighlighted = NO;
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    [self addSubview:btn];
    [self.btns addObject:btn];
    return btn;
}

/**
 *  按钮的位置
 */
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //按钮位置
    NSInteger count = self.btns.count;
    CGFloat btnW = self.size.width / count;
    CGFloat btnH = self.size.height;
    for (int i = 0; i < count; i ++) {
        UIButton *btn = self.btns[i];
        btn.x = i * btnW;
        btn.y = 0;
        btn.width = btnW;
        btn.height = btnH;
    }
    
}

- (void)setStatus:(Status *)status
{
    _status = status;
    [self setupBtnTitle:self.repostBtn count:status.reposts_count defaultTitle:@"转发"];
    [self setupBtnTitle:self.commentBtn count:status.comments_count defaultTitle:@"评论"];
    [self setupBtnTitle:self.attitudeBtn count:status.attitudes_count defaultTitle:@"赞"];
    
}

- (void)setupBtnTitle:(UIButton *)button count:(int)count defaultTitle:(NSString *)defaultTitle
{
    if (count >=10000) {
        defaultTitle = [NSString stringWithFormat:@"%d.1f万",count / 10000];
        defaultTitle = [defaultTitle stringByReplacingOccurrencesOfString:@".0" withString:@""];
    }else if (count > 0){
        defaultTitle = [NSString stringWithFormat:@"%d",count];
    }
    [button setTitle:defaultTitle forState:UIControlStateNormal];
}






@end
